package com.snacks;

import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.net.NetworkInfo.DetailedState;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.snacks.map.GoogleParser;
import com.snacks.map.MyPlaceRoom;
import com.snacks.map.Parser;
import com.snacks.map.Route;
import com.snacks.map.RouteOverlay;

public class Map extends MapActivity implements OnClickListener{
	MapView mapView = null;
	MapController mapController;
	private MapController mc;
	private GeoPoint p, startp, endp, geoPoint;	
	private Button btnNavigation, btBack;		

	double long1 = 21.013689 * 1E6;
	double lati1 = 105.802565 * 1E6;
	double dlongitude = 21.005997 * 1E6;
	double dlatitude = 105.805396 * 1E6;
	GeoPoint start, dest;
	
	private String id;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.map);		
		mapView = (MapView) findViewById(R.id.mapView);
		btnNavigation = (Button) findViewById(R.id.btNavigation);
		btBack = (Button) findViewById(R.id.btBack);
		
		btnNavigation.setOnClickListener(this);
		btBack.setOnClickListener(this);

		Bundle mBundle = getIntent().getExtras();
		if (mBundle != null) {
			String latitude = mBundle.getString("latitude");
			String longitude = mBundle.getString("longitude");
			id = mBundle.getString("id");

			Toast.makeText(
					this,
					"Dang dinh vi den toa do: (" + latitude + "," + longitude
							+ ")", Toast.LENGTH_LONG).show();

			dlongitude = Double.parseDouble(longitude) * 1E6;
			dlatitude = Double.parseDouble(latitude) * 1E6;
		}
		
		LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
		View zoomView = mapView.getZoomControls();

		zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		// Hien thi bo dieu khien zoom
		mapView.displayZoomControls(true);
		mc = mapView.getController();

		p = new GeoPoint((int) (dlongitude), (int) (dlatitude));

		startp = new GeoPoint((int) (long1), (int) (lati1));
		endp = new GeoPoint((int) (dlongitude), (int) (dlatitude));
		mc.animateTo(p);
		mc.setZoom(17);
		// ---Add a location marker---
		MapOverlay mapOverlay = new MapOverlay();
		List<Overlay> listOfOverlays = mapView.getOverlays();
		listOfOverlays.clear();
		listOfOverlays.add(mapOverlay);
		mapView.invalidate();

		btnNavigation.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mapView.removeAllViews();
				DirectionsTask getDirectionsTask = new DirectionsTask();
				getDirectionsTask.execute(new GeoPoint((int) (long1),
						(int) (lati1)), new GeoPoint((int) (dlongitude),
						(int) (dlatitude)));
			}
		});

	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btNavigation:
			mapView.removeAllViews();
			DirectionsTask getDirectionsTask = new DirectionsTask();
			getDirectionsTask.execute(new GeoPoint((int) (long1),
					(int) (lati1)), new GeoPoint((int) (dlongitude),
					(int) (dlatitude)));
			break;
		case R.id.btBack:
		
			finish();
			break;
		default:
			break;
		}
		
	}

	private class DirectionsTask extends AsyncTask<GeoPoint, Void, Route> {

		protected Route doInBackground(GeoPoint... geoPoints) {
			start = geoPoints[0];
			dest = geoPoints[1];

			Parser parser;
			String jsonURL = "http://maps.google.com/maps/api/directions/json?";
			final StringBuffer sBuf = new StringBuffer(jsonURL);
			sBuf.append("origin=");
			sBuf.append(start.getLatitudeE6() / 1E6);
			sBuf.append(',');
			sBuf.append(start.getLongitudeE6() / 1E6);
			sBuf.append("&destination=");
			sBuf.append(dest.getLatitudeE6() / 1E6);
			sBuf.append(',');
			sBuf.append(dest.getLongitudeE6() / 1E6);
			sBuf.append("&sensor=true&mode=driving");
			Log.v("I came in URL", sBuf.toString());
			parser = new GoogleParser(sBuf.toString());
			Route r = parser.parse();
			return r;
		}

		protected void onPostExecute(Route route) {

			Bitmap bmp = BitmapFactory.decodeResource(getResources(),
					R.drawable.pushpin);

			RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE,
					startp, endp, bmp);
			mapView.getOverlays().add(routeOverlay);
			mapView.getMapCenter();
			mapView.invalidate();
		}
	}

	class MapOverlay extends com.google.android.maps.Overlay {
		@Override
		public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
				long when) {
			super.draw(canvas, mapView, shadow);

			// ---translate the GeoPoint to screen pixels---
			Point screenPts = new Point();
			mapView.getProjection().toPixels(p, screenPts);

			// ---add the marker---
			Bitmap bmp = BitmapFactory.decodeResource(getResources(),
					R.drawable.pushpin);
			canvas.drawBitmap(bmp, screenPts.x, screenPts.y - bmp.getHeight(),
					null);
			return true;
		}

		@Override
		public boolean onTouchEvent(MotionEvent event, MapView mapView) {
			// ---when user lifts his finger---
			if (event.getAction() == 1) {
				GeoPoint p = mapView.getProjection().fromPixels(
						(int) event.getX(), (int) event.getY() - 30);
				Toast.makeText(
						getBaseContext(),
						p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6()
								/ 1E6, Toast.LENGTH_SHORT).show();
			}
			return false;
		}
	}

}
